package com.yxwl.demo.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.print.base.bean.PrinterConstantPool;
import com.print.printer.Command;
import com.print.printer.USBPrinterManage;
import com.yxwl.demo.R;
import com.yxwl.demo.ui.print.PrinterInfoActivity;
import com.yxwl.demo.ui.print.printer.PrinterUtil;
import com.yxwl.demo.utils.Utils;


public class MenuView extends FrameLayout implements View.OnClickListener {

    public MenuView(@NonNull Context context) {
        super(context);
    }

    public MenuView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MenuView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void init() {
        findViewById(R.id.menu_info).setOnClickListener(this);
        findViewById(R.id.menu_connect).setOnClickListener(this);
        findViewById(R.id.menu_disconnect).setOnClickListener(this);
        findViewById(R.id.print_test).setOnClickListener(this);
        setOnClickListener(this);
        if (PrinterUtil.getInstance().type== PrinterConstantPool.SocketType.USB){
            ((TextView) findViewById(R.id.menu_connect)).setText("开启服务");
            ((TextView) findViewById(R.id.menu_disconnect)).setText("关闭服务");
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.menu_info) {
            if (PrinterUtil.getInstance().isConnect()) {
                Intent intent = new Intent(Utils.getContext(), PrinterInfoActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Utils.getContext().startActivity(intent);
            }
        } else if (id == R.id.menu_connect) {
            if (PrinterUtil.getInstance().type==  PrinterConstantPool.SocketType.USB){
//                PermissionUtil.storagePermission(getContext(), "获取存储权限", true, new OnCallbackCode() {
//                    @Override
//                    public void onCallback(int code, Object o) {
//                        if (code == PermissionUtil.PERMISSION_STATUS_SUCCESS) {
//                            USBPrinterManage.getInstance().startUSBServer();
//                        }
//                    }
//                });
            }else {
                connect();
            }
        } else if (id == R.id.menu_disconnect) {
            if (PrinterUtil.getInstance().type==  PrinterConstantPool.SocketType.USB){
                USBPrinterManage.getInstance().stopUSBServer();
            }else {
                PrinterUtil.getInstance().disconnect();
            }
        } else if (id == R.id.print_test) {
            PrinterUtil.getInstance().printer.addTask(Command.print_SELFTEST(), false);
        }
        setVisibility(View.GONE);
    }

    public void connect() {
        LogView.log("连接中...");
        CustomProgress.showPD((Activity) getContext(), "连接中...");
        PrinterUtil.getInstance().connect();
    }



}
